<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="流程节点名称" prop="nodeName">
        <el-input
          v-model="queryParams.nodeName"
          placeholder="请输入流程节点名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="节点key" prop="processDefinitionKey">
        <el-input
          v-model="queryParams.processDefinitionKey"
          placeholder="请输入节点key"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>


    <el-table v-loading="loading" :data="bpmTaskAssigneeList">
      <el-table-column label="流程定义ID" align="center" prop="processDefinitionId" :show-overflow-tooltip="true"/>
      <el-table-column label="流程定义标识" align="center" prop="processDefinitionKey" />
      <el-table-column label="流程节点ID" align="center" prop="nodeId" />
      <el-table-column label="流程节点名称" align="center" prop="nodeName" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['flowable:bpmTaskAssignee:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['flowable:bpmTaskAssignee:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改流程定义设置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="60%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="流程定义id" prop="processDefinitionId">
              <el-input v-model="form.processDefinitionId" placeholder="请输入流程定义id" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="流程定义标识" prop="processDefinitionKey">
              <el-input v-model="form.processDefinitionKey" placeholder="请输入流程定义标识" disabled/>
            </el-form-item>
          </el-col>
        </el-row>
<!--        <el-form-item label="选择方式" prop="chooseWay">-->
<!--          <el-input v-model="form.chooseWay" placeholder="请输入选择方式" />-->
<!--        </el-form-item>-->
        <el-row>
          <el-col :span="12">
            <el-form-item label="流程节点id" prop="nodeId">
              <el-input v-model="form.nodeId" placeholder="请输入流程节点id" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="流程节点名称" prop="nodeName">
              <el-input v-model="form.nodeName" placeholder="请输入流程节点名称" disabled/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="表单类型" prop="formType">
              <el-select v-model="form.formType" placeholder="请选择表单类型" style="width: 100%">
                <el-option
                  v-for="item in formTypeList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12" v-if="form.formType == 0">
            <el-form-item label="表单" prop="formType">
              <el-select v-model="form.formId" placeholder="请选择表单" style="width: 100%">
                <el-option
                  v-for="item in bpmFormList"
                  :key="item.id"
                  :label="item.formName"
                  :value="item.id">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <template v-else >
            <el-col :span="12">
              <el-form-item label="自定义表单路径" prop="formCustomCreatePath">
                <el-input v-model="form.formCustomCreatePath" placeholder="请输入自定义表单创建路径" />
              </el-form-item>
            </el-col>

<!--            <el-col :span="8">-->
<!--              <el-form-item label="表单查看路径" prop="formCustomViewPath">-->
<!--                <el-input v-model="form.formCustomViewPath" placeholder="请输入自定义表单查看路径" />-->
<!--              </el-form-item>-->
<!--            </el-col>-->
          </template>

        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="审批人员" prop="assigneeId">
              <el-input placeholder="审批人员" v-model="form.assignee">
                <template slot="append"><el-button @click="userAssigneeSelect">审批人员</el-button></template>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>


<!--        <el-row>-->
<!--          <el-col :span="6">-->
<!--            <el-form-item label="弹窗选人" prop="dialogAssignee">-->
<!--              <el-switch-->
<!--                v-model="form.dialogAssignee"-->
<!--                :active-value="1"-->
<!--                :inactive-value="0">-->
<!--              </el-switch>-->
<!--            </el-form-item>-->
<!--          </el-col>-->
<!--          <el-col :span="6">-->
<!--            <el-form-item label="是否会签" prop="multiple">-->
<!--              <el-switch-->
<!--                v-model="form.multiple"-->
<!--                :active-value="1"-->
<!--                :inactive-value="0">-->
<!--              </el-switch>-->
<!--            </el-form-item>-->
<!--          </el-col>-->
<!--          <el-col :span="6">-->
<!--            <el-form-item label="是否可退回" prop="canBack">-->
<!--              <el-switch-->
<!--                v-model="form.canBack"-->
<!--                :active-value="1"-->
<!--                :inactive-value="0">-->
<!--              </el-switch>-->
<!--            </el-form-item>-->
<!--          </el-col>-->
<!--          <el-col :span="6">-->
<!--            <el-form-item label="是否可委托" prop="canDelegate">-->
<!--              <el-switch-->
<!--                v-model="form.canDelegate"-->
<!--                :active-value="1"-->
<!--                :inactive-value="0">-->
<!--              </el-switch>-->
<!--            </el-form-item>-->
<!--          </el-col>-->
<!--        </el-row>-->

<!--        <el-row>-->
<!--          <el-col :span="6">-->
<!--            <el-form-item label="是否可抄送" prop="canCopy">-->
<!--              <el-switch-->
<!--                v-model="form.canCopy"-->
<!--                :active-value="1"-->
<!--                :inactive-value="0">-->
<!--              </el-switch>-->
<!--            </el-form-item>-->
<!--          </el-col>-->
<!--          <el-col :span="6">-->
<!--            <el-form-item label="是否可加签" prop="canAddSignature">-->
<!--              <el-switch-->
<!--                v-model="form.canAddSignature"-->
<!--                :active-value="1"-->
<!--                :inactive-value="0">-->
<!--              </el-switch>-->
<!--            </el-form-item>-->
<!--          </el-col>-->
<!--          <el-col :span="6">-->
<!--            <el-form-item label="是否可减签" prop="canReduceSignature">-->
<!--              <el-switch-->
<!--                v-model="form.canReduceSignature"-->
<!--                :active-value="1"-->
<!--                :inactive-value="0">-->
<!--              </el-switch>-->
<!--            </el-form-item>-->
<!--          </el-col>-->
<!--          <el-col :span="6">-->
<!--            <el-form-item label="自动审批" prop="autoApproval">-->
<!--              <el-switch-->
<!--                v-model="form.autoApproval"-->
<!--                :active-value="1"-->
<!--                :inactive-value="0">-->
<!--              </el-switch>-->
<!--            </el-form-item>-->
<!--          </el-col>-->
<!--        </el-row>-->

<!--        <el-form-item label="审批人员" prop="assignee">-->
<!--          <el-input v-model="form.assignee" placeholder="请输入审批人员" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="审批人员id" prop="assigneeId">-->
<!--          <el-input v-model="form.assigneeId" placeholder="请输入审批人员id" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="业务规则id" prop="businessRuleId">-->
<!--          <el-input v-model="form.businessRuleId" placeholder="请输入业务规则id" />-->
<!--        </el-form-item>-->


<!--        <el-form-item label="会签保存人员KEY值" prop="multipleColumn">-->
<!--          <el-input v-model="form.multipleColumn" placeholder="请输入会签保存人员KEY值" />-->
<!--        </el-form-item>-->





<!--        <el-form-item label="任务监听" prop="taskListener">-->
<!--          <el-input v-model="form.taskListener" placeholder="请输入任务监听" />-->
<!--        </el-form-item>-->

<!--        <el-form-item label="表单ID" prop="formId">-->
<!--          <el-input v-model="form.formId" placeholder="请输入表单ID" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="表单option信息" prop="formOption">-->
<!--          <el-input v-model="form.formOption" type="textarea" placeholder="请输入内容" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="表单fields信息" prop="formFields">-->
<!--          <el-input v-model="form.formFields" type="textarea" placeholder="请输入内容" />-->
<!--        </el-form-item>-->


      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- tagSelect弹框 -->
    <tag-select-dialog :title="userListTitle" v-if="userListOpen"
                       :list-select-show="userListOpen"
                       :select-path="userListPath"
                       @getListValue="getUserListValue">
    </tag-select-dialog>

  </div>
</template>

<script>
import {
  listBpmTaskAssignee,
  getBpmTaskAssignee,
  delBpmTaskAssignee,
  addBpmTaskAssignee,
  updateBpmTaskAssignee,
  queryByNodeIdAndNodeKey
} from "@/api/flowable/bpmTaskAssignee";
import { listBpmForm } from "@/api/flowable/bpmForm";
import TagSelectDialog from "@/components/tagSelectDialog/tagSelectDialog.vue";

export default {
  name: "BpmTaskAssignee",
  components: {TagSelectDialog},
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 流程定义设置表格数据
      bpmTaskAssigneeList: [],
      // 弹出层标题
      title: "",
      formTypeList:[
        {
          label:"流程表单",
          value:0
        },
        {
          label: "自定义表单",
          value: 1
        }
      ],
      bpmFormList:[],
      userListPath:'flowable/bpmTaskAssignee/tagselect',
      //是否加载tagSelect弹框
      userListOpen:false,
      userListTitle:"审批人员选择",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        processDefinitionId: undefined,
        chooseWay: undefined,
        nodeId: undefined,
        nodeName: undefined,
        processDefinitionKey: undefined,
        assignee: undefined,
        assigneeId: undefined,
        businessRuleId: undefined,
        dialogAssignee: undefined,
        multiple: undefined,
        multipleColumn: undefined,
        canBack: undefined,
        canDelegate: undefined,
        canCopy: undefined,
        canAddSignature: undefined,
        canReduceSignature: undefined,
        taskListener: undefined,
        autoApproval: undefined,
        formId: undefined,
        formType: undefined,
        formOption: undefined,
        formFields: undefined,
        formCustomCreatePath: undefined,
        formCustomViewPath: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        id: [
          { required: true, message: "id不能为空", trigger: "blur" }
        ],
        processDefinitionId: [
          { required: true, message: "流程定义id不能为空", trigger: "blur" }
        ],
        chooseWay: [
          { required: true, message: "选择方式不能为空", trigger: "blur" }
        ],
        nodeId: [
          { required: true, message: "流程节点id不能为空", trigger: "blur" }
        ],
        nodeName: [
          { required: true, message: "流程节点名称不能为空", trigger: "blur" }
        ],
        processDefinitionKey: [
          { required: true, message: "节点key不能为空", trigger: "blur" }
        ],
        assignee: [
          { required: true, message: "审批人员不能为空", trigger: "blur" }
        ],
        assigneeId: [
          { required: true, message: "审批人员不能为空", trigger: "blur" }
        ],
        businessRuleId: [
          { required: true, message: "业务规则id不能为空", trigger: "blur" }
        ],
        dialogAssignee: [
          { required: true, message: "弹窗选人,0不弹,1弹窗不能为空", trigger: "blur" }
        ],
        multiple: [
          { required: true, message: "是否会签,0非会签,1会签不能为空", trigger: "blur" }
        ],
        multipleColumn: [
          { required: true, message: "会签保存人员KEY值不能为空", trigger: "blur" }
        ],
        canBack: [
          { required: true, message: "是否可退回,0不可退回,1可退回不能为空", trigger: "blur" }
        ],
        canDelegate: [
          { required: true, message: "是否可委托,0不可委托,1可委托不能为空", trigger: "blur" }
        ],
        canCopy: [
          { required: true, message: "是否可抄送,0不可抄送,1可抄送不能为空", trigger: "blur" }
        ],
        canAddSignature: [
          { required: true, message: "是否可加签,0不可加签,1可加签不能为空", trigger: "blur" }
        ],
        canReduceSignature: [
          { required: true, message: "是否可减签,0不可减签,1可减签不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.queryParams.processDefinitionId = this.$route.query.processDefinitionId;
    listBpmForm({}).then((res)=>{
      this.bpmFormList = res.rows;
    })
    this.getList();
  },
  methods: {
    /** 查询流程定义设置列表 */
    getList() {
      this.loading = true;
      listBpmTaskAssignee(this.queryParams).then(response => {
        this.bpmTaskAssigneeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        processDefinitionId: undefined,
        chooseWay: undefined,
        nodeId: undefined,
        nodeName: undefined,
        processDefinitionKey: undefined,
        assignee: undefined,
        assigneeId: undefined,
        businessRuleId: undefined,
        dialogAssignee: undefined,
        multiple: undefined,
        multipleColumn: undefined,
        canBack: undefined,
        canDelegate: undefined,
        canCopy: undefined,
        canAddSignature: undefined,
        canReduceSignature: undefined,
        taskListener: undefined,
        autoApproval: undefined,
        formId: undefined,
        formType: undefined,
        formOption: undefined,
        formFields: undefined,
        formCustomCreatePath: undefined,
        formCustomViewPath: undefined,
        createTime: undefined,
        updateTime: undefined,
        createBy: undefined,
        updateBy: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },


    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      queryByNodeIdAndNodeKey({  nodeId:row.nodeId, nodeName:row.nodeName, processDefinitionId:row.processDefinitionId}).then((res)=>{
        this.form = res.data;
        if(this.form.id){
          this.loading = false;
          this.open = true;
          this.title = "修改流程定义设置";
        }else {
          this.form.processDefinitionId = row.processDefinitionId;
          this.form.processDefinitionKey = row.processDefinitionKey;
          this.form.nodeId = row.nodeId;
          this.form.nodeName = row.nodeName;
          this.form.formType = 0 ;
          this.loading = false;
          this.open = true;
          this.title = "修改流程定义设置";
        }
      })

    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateBpmTaskAssignee(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addBpmTaskAssignee(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除流程定义设置编号为"' + ids + '"的数据项？').then(() => {
        this.loading = true;
        return delBpmTaskAssignee(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    getUserListValue(val){
      this.userListOpen = false;
      console.log(val)
      this.form.assigneeId=val.map(function(item) {return item.userId;}).join(',');
      this.form.assignee=val.map(function(item) {return item.userName;}).join(',');
    },
    userAssigneeSelect(){
      this.userListOpen = true;
    }
  }
};
</script>
